public  class WeekPlanEditCampaign {
    
    public String rtId{get;set;}
    public String rtHZId{get;set;}
    public Weekly_Plan__c wpc {get;set;}
    public List<Event> taskList {get;set;}
    public String strjson {get;set;}
    public Event t {get;set;}
    public RecordType rt {get;set;}
    public Boolean isEdit {get;set;}
    public String tabUrl; 
    public string View{get;set;}
    public WeekPlanEditCampaign(ApexPages.StandardController con) {
        // rt=[select id from RecordType where DeveloperName='Visit'];
        //Weekly_Plan__c w=(Weekly_Plan__c)con.getRecord();
        t=new Event();
      
        RecordType rtHZ=[select id from RecordType where DeveloperName='CamEvent'];
        RecordType rt=[select id from RecordType where DeveloperName='CamPlan'];
        
        rtId=rt.Id;
        rtHZId=rtHZ.Id;
        
        wpc=[select Id,Status__c,Name,OwnerId,Start_Date__c,End_Date__c,Budget__c,Current_Travel__c,Current_Business__c,Travel_Total__c,Business_Total__c from Weekly_Plan__c where id=:con.getId()];
        //taskList=new List<Event>();
        if(wpc.Status__c=='已提交'){View='none';}
        taskList=[SELECT WhatId,Is_View__c,Is_dele__c,
                  Campaigns__c,Campaigns__r.Name,
                  Opportunity__c,Opportunity__r.Name,
                  WhoId,Who.Name,
                  Account__c,Account__r.Name,
                  Subject,
                  Type,
                  Work_Address__c,
                  StartDateTime,EndDateTime,Week_Plan__c,Travel_Fee__c, Busness_Fee__c
                  FROM Event WHERE     
                  Week_Plan__c=:wpc.Id 
                                ORDER BY StartDateTime];
        
        //select出事件不能删除，只能新增
        for(Event eve:taskList)
        {
            eve.Is_View__c='none';
            eve.Is_dele__c='none';
            String sdtstr = String.valueOf(eve.StartDateTime);
            String edtstr = String.valueOf(eve.EndDateTime);
            eve.StartDateTimeStr__c=sdtstr.substring(0, sdtstr.length()-3);
            eve.EndDateTimeStr__c=edtstr.substring(0, edtstr.length()-3);
        }
        if(taskList.size()!=0)
        {
            taskList[taskList.size()-1].Is_View__c='';
        }
        if (System.today() > wpc.End_Date__c) {
            isEdit = false;
        } else {
            isEdit = true;
        }
       

    }

    public PageReference save(){
        
        update wpc;
        List<Object> inJsonMap = (List<Object>)JSON.deserializeUntyped(strjson);
        List<Event> taskIncomList=new List<Event>();
        for(Object obj : inJsonMap){
            Map<String,Object> objmap=(Map<String,Object>)obj;
            Event taskIncom=new Event();
            System.debug('*** objmap: ' + objmap + '');
            if(objmap.get('cam')+''!='')
            {
                taskIncom.WhatId=Id.valueOf(String.valueOf(objmap.get('cam')));
                taskIncom.Campaigns__c=Id.valueOf(String.valueOf(objmap.get('cam')));
                taskIncom.RecordTypeId=rtHZId;
            }
            if(objmap.get('enentId')!='')
            {
                taskIncom.Id=String.valueOf(objmap.get('enentId'));
            }
            taskIncom.Subject=String.valueOf(objmap.get('sub'));
            taskIncom.StartDateTime =DateTime.valueOf(''+objmap.get('startvisitdate')+':00');
            taskIncom.EndDateTime=DateTime.valueOf(''+objmap.get('endvisitdate')+':00');
            taskIncom.Week_Plan__c=wpc.Id;
            
            taskIncomList.add(taskIncom);

        }
        try{
            System.debug('*** taskIncomList: ' + taskIncomList + '');
            upsert taskIncomList Id;
            
        }catch(Exception e){
            
            ApexPages.addMessage(
            new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
            return null;
        }

        return new PageReference('/'+wpc.Id);
    }

    public PageReference cancle(){
        return new PageReference('/'+wpc.Id);
    }
}